<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\Auth\Permission;
use App\Models\Auth\User;
use App\Models\Common\ApiResponse;
use Illuminate\Http\Request;

class PermissionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    public function tree(Request $request)
    {
        $user = admin();

        if ($user->isSystemSuper) {

            if (getMerchantID(true)) {
                $allPermissions = Permission::where('guard_name', 'admin')->whereIdentity(User::IDENTITY_MERCHANT)->get()->toArray();
            } else {
//                if (getParkingLotID(false, true)) {
//                    $allPermissions = Permission::where('guard_name', 'admin')->whereIdentity(User::IDENTITY_PARKING_LOT)->get()->toArray();
//                } else {
//                    $allPermissions = Permission::where('guard_name', 'admin')->whereIdentity(User::IDENTITY_SYSTEM)->get()->toArray();
//                }
                $allPermissions = Permission::where('guard_name', 'admin')->where('identity', '!=', User::IDENTITY_MERCHANT)->get()->toArray(); // ->whereIdentity(User::IDENTITY_SYSTEM)
            }
        } else {
            $allPermissions = $user->permissionObjs->toArray();
        }

        $rawPermissions = Permission::mkTrees($allPermissions);

        foreach ($rawPermissions as $key => $rowPermissions) {
            if (\Arr::get($rowPermissions, 'parent_id')) {
                unset($rawPermissions[$key]);
            }
        }

        $permissionTrees = array_values($rawPermissions);
        Permission::fmtTrees($permissionTrees);
        return ApiResponse::return('', $permissionTrees);
    }

    public function wmpHomeMenus(Request $request)
    {
        $user = admin();

        $menus = [
            [
                'name' => '在场车辆',
                'value' => 'enter-log',
                'permission' => 'parking-lot.car.log.enter-log.list'
            ],
            [
                'name' => '出场车辆',
                'value' => 'out-log',
                'permission' => 'parking-lot.car.log.out-log.list'
            ],
            [
                'name' => '多位多车',
                'value' => 'many-space-enter-log',
                'permission' => 'parking-lot.car.log.enter-log.list'
            ],
            [
                'name' => '已锁车辆',
                'value' => 'lock-car',
                'permission' => 'parking-lot.car.log.lock-car.list'
            ],
            [
                'name' => '月租管理',
                'value' => 'mth-card',
                'permission' => 'parking-lot.car.mth-card.list'
            ],
            [
                'name' => '特殊车辆',
                'value' => 'store-car',
                'permission' => 'parking-lot.car.store-car.list'
            ],
            [
                'name' => '访客管理',
                'value' => 'visitor',
                'permission' => 'parking-lot.car.visitor.list'
            ],
            [
                'name' => '黑名单车辆',
                'value' => 'black',
                'permission' => 'parking-lot.car.warning-car.black.list'
            ],
            [
                'name' => '临时车报表',
                'value' => 'park-order',
                'permission' => 'parking-lot.count.park-order-paid'
            ],
            [
                'name' => '月租车报表',
                'value' => 'mth-order',
                'permission' => 'parking-lot.count.mth-order'
            ],
            [
                'name' => '设备状态报表',
                'value' => 'lane',
                'permission' => 'parking-lot.manage.lane.list'
            ],
        ];

        $user = $user->reassignPermissions();

        $list = [];
        foreach ($menus as $menu) {
            if ($user->can($menu['permission'])) {
                $list[] = $menu;
            }
        }

        return ApiResponse::success($list);
    }
}
